home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter4 / stats.c < prev    next >
C/C++ Source or Header  |  1996-03-21  |  10KB  |  290 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "stats.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLStatistics()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static HENV  hEnv  = NULL;
  111. static HDBC  hDBC  = NULL;
  112. static HWND  hList = NULL;
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE :
  117.               {
  118.                  RETCODE retCode;
  119.  
  120.                  // Allocate Environment and Connection.
  121.                  //.....................................
  122.                  SQLAllocEnv( &hEnv );
  123.                  SQLAllocConnect( hEnv, &hDBC );
  124.  
  125.                  // Connect to the data source
  126.                  //...........................
  127.                  retCode = SQLConnect( hDBC, 
  128.                              "Test Data Source", SQL_NTS,
  129.                              "DBA",              SQL_NTS,  
  130.                              "SQL",              SQL_NTS );
  131.  
  132.                  if ( retCode == SQL_SUCCESS )
  133.                  {
  134.                     int nTabSize = 60;
  135.  
  136.                     hList = CreateWindow( "LISTBOX", "",    
  137.                                LBS_NOTIFY | LBS_USETABSTOPS |
  138.                                LBS_NOINTEGRALHEIGHT  | LBS_SORT | 
  139.                                WS_BORDER  | WS_CHILD | 
  140.                                WS_VISIBLE | WS_VSCROLL, 
  141.                                0, 0, 0, 0, 
  142.                                hWnd, (HMENU)101,
  143.                                hInst, NULL );
  144.                     
  145.                     SendMessage( hList, LB_SETTABSTOPS, 
  146.                                  1, (LPARAM)&nTabSize );
  147.                  }
  148.               }
  149.               break;
  150.  
  151.       case WM_SIZE :
  152.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  153.                                        HIWORD( lParam ), TRUE );
  154.               break; 
  155.               
  156.       case WM_COMMAND :
  157.               switch( LOWORD( wParam ) )
  158.               {
  159.                  case IDM_TEST :
  160.                         {
  161.                            HSTMT   hStmt;
  162.                            SHORT   nNonUniqueIdx;
  163.                            UCHAR   szIndexName [128];
  164.                            UCHAR   szColumnName[128];
  165.                            UCHAR   szLBStr     [128];
  166.                            SDWORD  dwIndexLen;
  167.                            SDWORD  dwColumnLen;
  168.                            RETCODE retCode;
  169.  
  170.                            // Allocate a statement handle
  171.                            // to receive the result set.
  172.                            // ...........................
  173.                            SQLAllocStmt( hDBC, &hStmt );
  174.  
  175.                            // Call SQLStatistics() to request all 
  176.                            // unique indexes associated with the 
  177.                            // 'customer' table.
  178.                            // ...................................
  179.                            retCode = SQLStatistics( hStmt, 
  180.                                         NULL,       SQL_NTS, 
  181.                                         NULL,       SQL_NTS, 
  182.                                         "customer", SQL_NTS,
  183.                                         SQL_INDEX_ALL,
  184.                                         SQL_ENSURE );
  185.  
  186.                            if( retCode == SQL_SUCCESS || 
  187.                                retCode == SQL_SUCCESS_WITH_INFO )
  188.                            {
  189.                               // Bind a column to the INDEX_NAME and
  190.                               // COLUMN_NAME columns in the result 
  191.                               // set generated from SQLStatistics().
  192.                               // ...................................
  193.                               SQLBindCol( hStmt, 4, SQL_C_SHORT, 
  194.                                  &nNonUniqueIdx, sizeof( nNonUniqueIdx ), 
  195.                                  NULL );
  196.  
  197.                               SQLBindCol( hStmt, 6, SQL_C_CHAR, 
  198.                                  szIndexName, sizeof( szIndexName ), 
  199.                                  &dwIndexLen );
  200.  
  201.                               SQLBindCol( hStmt, 9, SQL_C_CHAR, 
  202.                                  szColumnName, sizeof( szColumnName ), 
  203.                                  &dwColumnLen );
  204.  
  205.                               // Fetch all the records and
  206.                               // display them in the list box.
  207.                               //..............................
  208.                               retCode = SQLFetch( hStmt );
  209.  
  210.                               while ( retCode == SQL_SUCCESS )
  211.                               {
  212.                                  if( dwColumnLen > 0 && dwIndexLen  > 0 )
  213.                                  {
  214.                                     // Set string contents to be  
  215.                                     // index name and column name.
  216.                                     // ...........................
  217.                                     szLBStr[0] = '\0';
  218.                                     strcat( szLBStr, szIndexName ); 
  219.                                     strcat( szLBStr, "\t" );
  220.                                     strcat( szLBStr, szColumnName );
  221.                                     strcat( szLBStr, "\t" );
  222.                                     strcat( szLBStr, nNonUniqueIdx ? 
  223.                                        "Not unique" : "Unique" );
  224.  
  225.                                     SendMessage( hList, LB_ADDSTRING, 
  226.                                                  0, (LPARAM)szLBStr );
  227.                                  }
  228.  
  229.                                  retCode = SQLFetch( hStmt );
  230.                               }
  231.                            }
  232.  
  233.                            SQLFreeStmt( hStmt, SQL_DROP );
  234.                         }
  235.                         break;
  236.  
  237.                  case IDM_ABOUT :
  238.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  239.                         break;
  240.  
  241.                  case IDM_EXIT :
  242.                         DestroyWindow( hWnd );
  243.                         break;
  244.               }
  245.               break;
  246.       
  247.       case WM_DESTROY :
  248.               PostQuitMessage(0);
  249.  
  250.               // Disconnect Environment.
  251.               //........................
  252.               SQLDisconnect( hDBC );
  253.  
  254.               // Free Connection and Environment.
  255.               //.................................
  256.               SQLFreeConnect( hDBC );
  257.               SQLFreeEnv( hEnv );
  258.               break;
  259.  
  260.       default :
  261.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  262.    }
  263.  
  264.    return( 0L );
  265. }
  266.  
  267.  
  268. LRESULT CALLBACK About( HWND hDlg,           
  269.                         UINT message,        
  270.                         WPARAM wParam,       
  271.                         LPARAM lParam)
  272. {
  273.    switch (message) 
  274.    {
  275.        case WM_INITDIALOG: 
  276.                return (TRUE);
  277.  
  278.        case WM_COMMAND:                              
  279.                if (   LOWORD(wParam) == IDOK         
  280.                    || LOWORD(wParam) == IDCANCEL)    
  281.                {
  282.                        EndDialog(hDlg, TRUE);        
  283.                        return (TRUE);
  284.                }
  285.                break;
  286.    }
  287.  
  288.    return (FALSE); 
  289. }
  290.